package com.jacobson.huobi.components.header

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.jacobson.huobi.R
import com.jacobson.huobi.common.expansion.toast
import com.jacobson.huobi.utils.ResourcesUtil
import kotlinx.android.synthetic.main.view_common_header.view.*

/**
 * ================================================
 *  通用头部
 *  @author Wucz on 2018/8/3 9:56
 * ================================================
 */
class CommonHeader @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    private var title: String = ""
    private var onMoreClickListener: OnMoreClickListener? = null

    override fun onFinishInflate() {
        super.onFinishInflate()
        LayoutInflater.from(context).inflate(R.layout.view_common_header, this, true)
        commonHeaderTvBack.setOnClickListener {
            (context as? Activity)?.finish()
        }
        commonHeaderTvTitle.text = title
        commonHeaderTvMore.setOnClickListener {
            onMoreClickListener?.onMoreClick()
        }
    }

    fun setTitle(title: String) {
        this.title = title
        commonHeaderTvTitle?.text = title
    }

    fun setTitle(resId: Int) {
        setTitle(context.getString(resId))
    }

    private fun setOnMoreClickListener(listener: () -> Unit) {
        setOnMoreClickListener(object : OnMoreClickListener {
            override fun onMoreClick() {
                listener()
            }
        })
    }

    private fun setOnMoreClickListener(listener: OnMoreClickListener) {
        commonHeaderTvMore.visibility = View.VISIBLE
        this.onMoreClickListener = listener
    }

    interface OnMoreClickListener {
        fun onMoreClick()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        setPadding(paddingLeft, paddingTop, paddingRight, 0)
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

    //是否支持分享
    fun setShareable(shareable: Boolean) {
        setOnMoreClickListener {
            if (shareable) {
                context.toast("暂未实现分享功能")
            }
        }
    }

    fun setDarkMode() {
        commonHeaderTvTitle.setTextColor(ResourcesUtil.getColor(R.color.textColorPrimary))
        commonHeaderTvBack.setImageResource(R.mipmap.ic_common_back_dark)
    }

    fun setTitleAlpha(alpha: Float) {
        commonHeaderTvTitle.alpha = alpha
    }
}